package aircraft_war;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class Main extends JPanel {
    private static int state=0;
    private static final int START = 0;
    private static final int RUNNING = 1;
    private static final int PAUSE = 2;
    private static final int GAME_OVER = 3;

    static BufferedImage bg=null;
    static BufferedImage airPlane=null;
    static BufferedImage airPlane0=null;
    static BufferedImage airPlane1=null;
    static BufferedImage bigPlane=null;
    static BufferedImage bee=null;
    static BufferedImage bullet=null;
    static BufferedImage start=null;
    static BufferedImage pause=null;
    static BufferedImage gameover=null;
    static BufferedImage hero0=null;
    static BufferedImage hero1=null;
    static BufferedImage boss0=null;
    static BufferedImage boss1=null;

    static {
        try {
            bg= ImageIO.read(Main.class.getResourceAsStream("Image/background.png"));
            airPlane= ImageIO.read(Main.class.getResourceAsStream("Image/airplane.png"));
            airPlane0= ImageIO.read(Main.class.getResourceAsStream("Image/airplane_ember0.png"));
            airPlane1= ImageIO.read(Main.class.getResourceAsStream("Image/airplane_ember1.png"));
            bigPlane= ImageIO.read(Main.class.getResourceAsStream("Image/bigplane.png"));
            bee= ImageIO.read(Main.class.getResourceAsStream("Image/bee.png"));
            hero0= ImageIO.read(Main.class.getResourceAsStream("Image/hero0.png"));
            hero1= ImageIO.read(Main.class.getResourceAsStream("Image/hero1.png"));
            bullet= ImageIO.read(Main.class.getResourceAsStream("Image/bullet.png"));
            start= ImageIO.read(Main.class.getResourceAsStream("Image/start.png"));
            pause= ImageIO.read(Main.class.getResourceAsStream("Image/pause.png"));
            gameover= ImageIO.read(Main.class.getResourceAsStream("Image/gameover.png"));
            boss0= ImageIO.read(Main.class.getResourceAsStream("Image/extraplane_n1.png"));
            boss1= ImageIO.read(Main.class.getResourceAsStream("Image/extraplane_n2.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    Timer timer=new Timer();
    HeroPlane hero=new HeroPlane();
    ArrayList<Bullet> bull=new ArrayList<>();
    ArrayList<BasicClass> flyings=new ArrayList();


    //int count=0;
    public void paintState(Graphics g) {
        switch (state) {
            case START: // 启动状态
                g.drawImage(start, 0, 0, null);
                break;
            /*case RUNNING: // 运行状态
                g.drawImage(bg, 0, 0, null);
                break;*/
            case PAUSE: // 暂停状态
                g.drawImage(pause, 0, 0, null);
                break;
            case GAME_OVER: // 游戏终止状态
                g.drawImage(gameover, 0, 0, null);
                break;
        }
    }
    public void action(){
        MouseAdapter l = new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) { // 鼠标移动
                if (state == RUNNING) { // 运行状态下移动英雄机--随鼠标位置
                    /*int x = e.getX();
                    int y = e.getY();
                    hero.moveTo(x, y);*/
                    hero.setX(e.getX()-hero.width/2);
                    hero.setY(e.getY()-hero.height/2);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) { // 鼠标进入
                if (state == PAUSE) { // 暂停状态下运行
                    state = RUNNING;
                }
            }

            @Override
            public void mouseExited(MouseEvent e) { // 鼠标退出
                if (state != GAME_OVER&&state!=START) { // 游戏未结束，则设置其为暂停
                    state = PAUSE;
                }
            }

            @Override
            public void mouseClicked(MouseEvent e) { // 鼠标点击
                switch (state) {
                    case START:
                        state = RUNNING; // 启动状态下运行
                        break;
                    case GAME_OVER: // 游戏结束，清理现场
                        flyings = new ArrayList(); // 清空飞行物
                        bull = new ArrayList(); // 清空子弹
                        hero = new HeroPlane(); // 重新创建英雄机
                        hero.score = 0; // 清空成绩
                        state = START; // 状态设置为启动
                        break;
                }
            }
        };
        this.addMouseListener(l); // 处理鼠标点击操作
        this.addMouseMotionListener(l); // 处理鼠标滑动操作
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (state==1){
                    createFlying();
                    runFlying();
                    outFlying();
                    createBullet();
                    runBullet();
                    outBullet();
                    banBull();
                    banHero();
                }
                repaint();
            }
        }, 100, 42);
    }
    int count=0;
    private void createBullet(){
        count++;
        if (count%10==0){
            Bullet[] bs=hero.shoot();
            for (int i = 0; i < bs.length; i++) {
                bull.add(bs[i]);
            }
        }
    }

    private void paintBullet(Graphics g){
        for (int i = 0; i < bull.size(); i++) {
            Bullet n=bull.get(i);
            g.drawImage(n.getImg(),n.getX(),n.getY(),this);
        }
    }
    private void runBullet(){
        for (int i = 0; i < bull.size(); i++) {
            Bullet fly=bull.get(i);
            fly.move();
        }
    }
    private void outBullet(){
        for (int i = 0; i < bull.size(); i++) {
            Bullet fly=bull.get(i);
            if (fly.getY()<0){
                bull.remove(fly);
                i--;//回到当前位置,因为remove后会自动补位
            }
        }
    }
    int flyIndex;
    private void createFlying() {
        flyIndex++;
        if(flyIndex%20==0){
            int ran=(int)(Math.random()*50);
            BasicClass fly = null;
            if (ran<5){
                fly=new Bee();
            }
            else if (ran>=40&&ran!=7){
                fly=new BigPlane();
            }
            else if(ran==7){
                fly=new Boss();
            }
            else
            {
                fly=new AirPlane();
            }
            flyings.add(fly);
        }


    }

    private void paintFlying(Graphics g){
        for (int i = 0; i < flyings.size(); i++) {
            BasicClass n=flyings.get(i);
            g.drawImage(n.getImg(),n.getX(),n.getY(),this);
        }
    }
    private void runFlying(){
        for (int i = 0; i < flyings.size(); i++) {
            BasicClass fly=flyings.get(i);
            fly.move();

        }
    }
    private void outFlying(){
        for (int i = 0; i < flyings.size(); i++) {
            BasicClass fly=flyings.get(i);
            if (fly.getY()>HEIGHT){
                flyings.remove(fly);
                i--;//回到当前位置
            }
        }
    }
    //循环所有子弹
    public void banBull(){
        for (int i = 0; i < bull.size(); i++) {
            Bullet bul=bull.get(i);
            //将每一个子弹都与飞行物进行检测
            if(ban(bul)==true){
                bull.remove(i);
                i--;
            }
        }
    }

    //循环出的子弹检测与所有飞行物的碰撞
    public boolean ban(Bullet bul){
        for (int i = 0; i < flyings.size(); i++) {
            BasicClass fly=flyings.get(i);
            if (fly.getY()+fly.getHeight()>=bul.getY()&&fly.getX()<+bul.getX()&&fly.getX()+fly.getWidth()>=bul.getX()&&fly.getY()<=bul.getY()){
                fly.blood--;
                //bull.remove(num);
                if (fly.blood<=0){
                    if (fly instanceof Bee){//判断是不是蜜蜂类型
                        hero.choice();
                       /* hero.blood++;
                        if (hero.flag==1){
                            hero.flag=2;
                        }
                        else if (hero.flag==2){
                            hero.flag=3;
                        }*/
                    }
                    if (fly instanceof AddScore){
                        AddScore add= (AddScore)fly;
                        hero.score=hero.score+add.addScore();
                    }

                    /*
                    也可以下转后
                    AirPlane air=AirPlane(fly);
                    hero.score=air.score+hero.score;
                    */
                    //fly.destory();
                    fly.getX();
                    fly.getY();
                    flyings.remove(i);

                }
                return true;
            }
        }
        return false;

    }
    public void banHero(){

        for (int i = 0; i < flyings.size(); i++) {
            BasicClass fly=flyings.get(i);
            if (fly.getY()+fly.getHeight()>=hero.getY()&&fly.getX()<=hero.getX()+hero.width&&fly.getX()+fly.getWidth()>=hero.getX()&&fly.getY()<=hero.getY()+hero.height){
                fly.blood-=3;
                hero.blood--;
                //bull.remove(num);
                if (fly.blood<=0){
                    flyings.remove(i);
                }
                if (hero.blood<=0){
                    state=GAME_OVER;
                }
            }
        }

    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);

        //显示图片
        g.drawImage(bg,0,0,this);
       // g.drawImage(start,WIDTH/2-start.getWidth()/2,HEIGHT/2-start.getHeight()/2,this);

        //drawBullet(g);
        paintFlying(g);
        paintBullet(g);
        drawHeroPlane(g);
        paintState(g);



        //显示文字
        Font font=new Font("黑体",Font.BOLD,15);
        g.setFont(font);
        Color c=Color.BLACK;
        g.setColor(c);
        g.drawString("Score:"+hero.score,5,20);
        g.drawString("Life:"+hero.blood,5,50);
    }
    /*private void drawStart(Graphics g) {
        g.drawImage(start,240-start.getWidth()/2,415-start.getHeight()/2,this);
    }*/
    /*private void drawBullet(Graphics g) {
        for(int i = 0; i < bull.size(); i++) {
            bull.get(i).move();
            g.drawImage(bull.get(i).getImg(),bull.get(i).getX()-3,bull.get(i).getY(),this);

        }
    }*/

    private void drawHeroPlane(Graphics g) {
        hero.move();
        g.drawImage(hero.getImg(),hero.getX(),hero.getY(),this);
    }


    public  static final int WIDTH=410;
    public  static final int HEIGHT=700;

    public static void main(String[] args) {
        JFrame win =new JFrame();
        Main main=new Main();
        win.add(main);
        main.action();
        //z设置窗口大小
        win.setSize(WIDTH,HEIGHT);
        //读取屏幕尺寸
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        //移动窗口位置
        win.setLocation((dim.width - win.getWidth()) / 2, 0);
        win.setDefaultCloseOperation(3);
        win.setVisible(true);
    }


}
